<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Is return_internal_reference efficient?</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python">
<link rel="up" href="../faq.html" title="Chapter 5. Frequently Asked Questions (FAQs)">
<link rel="prev" href="i_m_getting_the_attempt_to_retur.html" title="I'm getting the &quot;attempt to return dangling reference&quot; error. What am I doing wrong?">
<link rel="next" href="how_can_i_wrap_functions_which_t.html" title="How can I wrap functions which take C++ containers as arguments?">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="i_m_getting_the_attempt_to_retur.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="how_can_i_wrap_functions_which_t.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="faq.is_return_internal_reference_eff"></a><a class="link" href="is_return_internal_reference_eff.html" title="Is return_internal_reference efficient?">Is <code class="computeroutput"><span class="identifier">return_internal_reference</span></code> efficient?</a>
</h3></div></div></div>
<p>
        <span class="bold"><strong>Q:</strong></span> /I have an object composed of 12 doubles.
        A <code class="computeroutput"><span class="keyword">const</span><span class="special">&amp;</span></code>
        to this object is returned by a member function of another class. From the
        viewpoint of using the returned object in Python I do not care if I get a
        copy or a reference to the returned object. In Boost.Python I have the choice
        of using <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
        or <code class="computeroutput"><span class="identifier">return_internal_reference</span></code>.
        Are there considerations that would lead me to prefer one over the other,
        such as size of generated code or memory overhead?/
      </p>
<p>
        <span class="bold"><strong>A:</strong></span> <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
        will make an instance with storage for one of your objects, <code class="computeroutput"><span class="identifier">size</span> <span class="special">=</span> <span class="identifier">base_size</span> <span class="special">+</span> <span class="number">12</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">double</span><span class="special">)</span></code>.
        <code class="computeroutput"><span class="identifier">return_internal_reference</span></code>
        will make an instance with storage for a pointer to one of your objects,
        <code class="computeroutput"><span class="identifier">size</span> <span class="special">=</span>
        <span class="identifier">base_size</span> <span class="special">+</span>
        <span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">void</span><span class="special">*)</span></code>. However,
        it will also create a weak reference object which goes in the source object's
        weakreflist and a special callback object to manage the lifetime of the internally-referenced
        object. My guess? <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
        is your friend here, resulting in less overall memory use and less fragmentation,
        also probably fewer total cycles.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2015 David
      Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="i_m_getting_the_attempt_to_retur.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="how_can_i_wrap_functions_which_t.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>
